package com.chenfan.mcn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.UnPayBackContractDTO;
import com.chenfan.mcn.model.vo.*;
import com.chenfan.mcn.service.DataStatisticsService;
import com.chenfan.privilege.common.config.SearchAuthority;
import com.chenfan.mcn.model.common.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 首页数据统计
 * @author: tangwei
 * Date:     2021/3/9 9:45
 * Description: 首页数据统计
 */
@RequestMapping("/dataStatistics")
@RestController
public class DataStatisticsController {

    @Autowired
    private DataStatisticsService dataStatisticsService;


    /**
     * 客户投放金额（本月）
     * @param page 分页
     * @return
     */
    @GetMapping(value = "/customerInvestmentAmount")
    public Res<Page<CustomerInvestmentAmount>> customerInvestmentAmount(@ApiIgnore McnPage<CustomerInvestmentAmount> page) {
        return Res.ok(dataStatisticsService.customerInvestmentAmount(page));
    }


    /**
     * 红人月度执行单数量&金额（本月）
     * @param type
     * @param page
     * @return
     */
    @GetMapping(value = "/redManMonthlyExecution")
    public Res<Page<RedManMonthlyExecution>> redManMonthlyExecution(Integer type, @ApiIgnore McnPage<RedManMonthlyExecution> page) {
        if (Objects.isNull(type)) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(dataStatisticsService.redManMonthlyExecution(type, page));
    }


    /**
     * 合同签订情况（本月）
     *
     * @return {@link List<ContractSigning>}
     */
    @GetMapping(value = "/contractSigning")
    public Res<List<ContractSigning>> contractSigning() {
        return Res.ok(dataStatisticsService.contractSigning());
    }

    /**
     * 部门业绩情况(本月)
     *
     * @return {@link List<DepartmentPerformance>}
     */
    @GetMapping(value = "/departmentPerformance")
    public Res<List<DepartmentPerformance>> departmentPerformance() {
        return Res.ok(dataStatisticsService.departmentPerformance());
    }

    /**
     * 签订收入合同总金额
     * @return
     */
    @GetMapping("/incomeContract/totalAmount")
    public Res<BigDecimal> incomeContractTotalAmount() {
        return Res.ok(dataStatisticsService.incomeContractTotalAmount());
    }

    /**
     * 执行单已执行金额
     * @return
     */
    @GetMapping("/excutionOrder/excutedAmount")
    public Res<BigDecimal> excutedAmount() {
        return Res.ok(dataStatisticsService.excutedAmount());
    }

    /**
     * 执行单执行数量
     * @param type
     * @return
     */
    @GetMapping("/excutionOrder/totalCount")
    public Res<List<ExcutionOrderCountVO>> totalCount(@RequestParam Integer type) {
        if (type == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(dataStatisticsService.totalCount(type));
    }

    /**
     * 首页-未回款合同
     * @param unPayBackContractDTO
     * @return
     */
    @GetMapping("/incomeContract/unPayBackContract")
    public Res<PageInfo<UnPayBackContractVO>> unPayBackContract(@SearchAuthority UnPayBackContractDTO unPayBackContractDTO) {
        return Res.ok(dataStatisticsService.unPayBackContract(unPayBackContractDTO));
    }

    /**
     * 首页-累计回款金额、累计未回款金额、累计签单金额
     * @param unPayBackContractDTO
     * @return
     */
    @GetMapping("/incomeContract/grandAmount")
    public Res<GrandAmountVO> grandAmount(@SearchAuthority UnPayBackContractDTO unPayBackContractDTO) {
        return Res.ok(dataStatisticsService.grandAmount(unPayBackContractDTO));
    }

    /**
     * 首页-未回款排行
     * @param unPayBackRankDTO
     * @return
     */
    @GetMapping("/incomeContract/unPayBackRank")
    public Res<PageInfo<UnPayBackRankVO>> unPayBackRank(UnPayBackContractDTO unPayBackRankDTO) {
        return Res.ok(dataStatisticsService.unPayBackRank(unPayBackRankDTO));
    }


    /**
     * 首页-合品牌作排名
     *
     * @param page 分页
     * @return
     */
    @GetMapping("/ranking/cooperativeBrands")
    public Res<Page<RankingOfCooperativeBrands>> rankingOfCooperativeBrands(@ApiIgnore McnPage<RankingOfCooperativeBrands> page) {
        return Res.ok(dataStatisticsService.rankingOfCooperativeBrands(page));
    }


    /**
     * 首页-当前季度部门回款率
     *
     * @param page 分页
     * @return
     */
    @GetMapping("/department/collectionRate")
    public Res<Page<DepartmentCollectionRate>> departmentCollectionRate(@ApiIgnore McnPage<DepartmentCollectionRate> page) {
        return Res.ok(dataStatisticsService.departmentCollectionRate(page));
    }
}